const app = getApp()
import api from '../../utils/api'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //等待框
    waitHidden: true,
    navBarHeight: app.globalData.navBarHeight, //导航栏高度
    menuBotton: app.globalData.menuBotton, //导航栏距离顶部距离
    menuHeight: app.globalData.menuHeight, //导航栏高度
    //页面尺寸数据
    windowHeight: 0,
    screenHeight: 0,
    feedbackDetail: [],
    show: false,
    quoteText: "",
    replyFeedback: "",
    feedbackId: 0,
    inputVal: "",
    idx: "",
    moduleHide: true,
    moduleType: "",
    modules: [{
        id: "01",
        text: "保障支持负责人"
      },
      {
        id: "02",
        text: "人力资源负责人"
      },
      {
        id: "03",
        text: "资源管理负责人"
      },
      {
        id: "04",
        text: "财务管理负责人"
      },
      {
        id: "05",
        text: "园区管理负责人"
      },
      {
        id: "06",
        text: "其他问题负责人"
      },
    ],
    sendFlag: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const _this = this
    wx.hideShareMenu()
    //设置屏幕尺寸数据
    this.setData({
      windowHeight: app.globalData.windowHeight,
      screenHeight: app.globalData.screenHeight
    })
    /**
     * 通过传过来的id获取feedback
     */
    if (options.feedbackId) {
      _this.setData({
        feedbackId: options.feedbackId
      })
    }
    if (options.idx) {
      _this.setData({
        idx: options.idx
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const _this = this
    this.setData({
      pageNo: 1,
    })
    wx.request({
      url: api.cloudGetDetails,
      // url: "https://bc-test.542124141.xyz/cloud/common/getDetails",
      method: "POST",
      data: {
        id: _this.data.feedbackId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        _this.setData({
          feedbackDetail: res.data.data
        })
      }
    })
    /*
     * 更新为已读状态
     */
    wx.request({
      url: api.cloudUpdateQuestionReadStatus,
      method: "POST",
      data: {
        id: _this.data.feedbackId,
        manager: true
      },
      header: {
        'content-type': 'application/json' // 默认值
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},

  /**
   * 返回上一页
   */
  goBack: function (e) {
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 跳转到首页
   */
  goToIdx: function (e) {
    wx.navigateTo({
      url: '/pages/feedback/feedback'
    })
  },
  /**
   * 预览图片
   */
  previewImage: function (e) {
    wx.previewImage({
      urls: [e.currentTarget.dataset.imgurl],
    })
  },
  /**
   * 绑定输入内容
   */
  commentInput: function (e) {
    this.setData({
      replyFeedback: e.detail.value
    })
  },
  /**
   * 发送反馈
   */
  sendClick: function () {
    var that = this
    if (this.data.replyFeedback == "") {
      wx.showToast({
        title: '请输入反馈',
        icon: 'error'
      })
    } else {
      if (this.data.sendFlag) {
        this.setData({
          sendFlag: false
        })
        wx.request({
          url: api.cloudAddFeedback,
          method: "POST",
          data: {
            moduleType: that.data.feedbackDetail[0].moduleType,
            content: that.data.replyFeedback,
            id: that.data.feedbackDetail[0].id,
            userType: wx.getStorageSync('logindata').data.userType,
            pagepath: '/pages/feedbackDetail/feedbackDetail?idx=true&feedbackId=' + that.data.feedbackDetail[0].id
          },
          header: {
            'content-type': 'application/json',
            'token': wx.getStorageSync('token')
          },
          success(e) {
            if (e.statusCode == 200) {
              wx.showToast({
                title: '提交反馈成功！',
              })
              that.setData({
                inputVal: "",
                replyFeedback: ""
              })
              that.onShow()
            } else {
              wx.showToast({
                title: '提交反馈失败' + e.statusCode,
                icon: 'error'
              })
            }
            that.setData({
              sendFlag: true
            })
          }
        })
      }
    }
  },

  // 显示遮罩层
  showModal: function (e) {
    var that = this;

    that.setData({
      moduleHide: false,
    })
    var animation = wx.createAnimation({
      duration: 600, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
      timingFunction: 'ease', //动画的效果 默认值是linear
    })
    this.animation = animation
    setTimeout(function () {
      that.fadeIn(); //调用显示动画
    }, 200)
  },

  // 隐藏遮罩层
  hideModal: function () {
    var that = this;
    var animation = wx.createAnimation({
      duration: 800, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
      timingFunction: 'ease', //动画的效果 默认值是linear
    })
    this.animation = animation
    that.fadeDown(); //调用隐藏动画   
    setTimeout(function () {
      that.setData({
        moduleHide: true,
      })
    }, 750) //先执行下滑动画，再隐藏模块
  },

  //动画集
  fadeIn: function () {
    this.animation.translateY(0).step()
    this.setData({
      animationData: this.animation.export() //动画实例的export方法导出动画数据传递给组件的animation属性
    })
  },
  fadeDown: function () {
    this.animation.translateY(700).step()
    this.setData({
      animationData: this.animation.export(),
    })
  },

  // 选择对象
  moduleRadioChange: function (e) {
    this.setData({
      moduleType: e.detail.value,
    })
  },

  // 确认转交
  forwardConfirm: function (e) {
    const that = this
    wx.request({
      url: api.cloudChangeQuestionType,
      // url: "https://bc-test.542124141.xyz/cloud/manager/changeQuestionType",
      method: "POST",
      data: {
        id: that.data.feedbackDetail[0].id,
        moduleType: that.data.moduleType,
        pagepath: '/pages/feedbackDetailReply/feedbackDetailReply?idx=true&feedbackId=' + that.data.feedbackDetail[0].id
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success(e) {
        if (e.statusCode == 200) {
          wx.showToast({
            title: '转交成功！',
          })
          // that.hideModal()
          wx.navigateTo({
            url: '/pages/feedbackAll/feedbackAll?isReplied=f'
          })
        } else {
          wx.showToast({
            title: '转交失败' + e.statusCode,
            icon: 'error'
          })
        }
      }
    })
  }
})